import pandas as pd
import csv
import random

file = "Items_Info.csv"
filedata = pd.read_csv(file)
D1 = filedata["Product_Name"]
D2 = filedata["Cost"]
D3 = filedata["Price"]
Ds = {"ProductName":D1,"Cost":D2,"Price":D3}
I = pd.DataFrame(Ds)
I.to_csv('Items_Info_clean.csv',index = False)

wage = round(6.4658 * 656,2)

#P
def Calculate_P (market_share,pwr,pcr):
    P = 0
    if market_share >= 5 and market_share <= 10:
        P += 1
    elif market_share >= 1 and market_share < 5:
        P += 0.5
    elif market_share < 1:
        P += 0.25

    #PWR:price_wage_ratio
    if pwr < 0.0005:
        P += 2
    elif pwr >= 0.0005 and pwr < 0.01:
        P += 1
    elif pwr >= 0.01 and pwr < 0.05:
        P += 0.5

    #PCR:proft_cost_ratio
    if pcr > 0.05:
        P += 2
    elif pcr >= 0.01 and pcr < 0.05:
        P += 1
    elif pcr < 0.01:
        P += 0.5

    return P


file2 = "Items_Info_clean.csv"
fileData = pd.read_csv(file2)
Ps = []
PWRs = []
PCRs = []
MSs = []

for i in range(0,len(fileData)):
    I = fileData.loc[i]
    cost = fileData["Cost"][i]
    price = fileData["Price"][i]
    MS = round(random.uniform(0.0005,0.1) , 4)
    PWR = round(price / wage , 4)
    PCR = round((price - cost) / cost , 4)
    PWRs.append(PWR)
    PCRs.append(PCR)
    MSs.append(MS)
    P = Calculate_P(MS,PWR,PCR)
    Ps.append(P)

#print(Ps)
#print(PWRs)

data = pd.read_csv("InfoOfMaterials.csv",encoding="gb18030")
dfc = data["DFC_score"]
dfc = dfc.tolist()
dfc.insert(0," ")

Total = 0
Ss = []
temp = []
spaceIndex = []

for i in range(len(dfc)):
    if dfc[i] == " ":
        spaceIndex.append(i)

#print(spaceIndex)
s = 0

nums = []

for j in range(len(spaceIndex)):
    if j == len(spaceIndex)-1:
        index = spaceIndex[-1]
        temp = dfc[index+1:-1]
        
        num = len(dfc)-j
        
        for r in range(len(temp)):
            t = int(temp[r])
            s += t
        Ss.append(s)

    else:
        index1,index2 = spaceIndex[j],spaceIndex[j+1]
        temp = dfc[index1+1:index2]
        s = 0
        
        num = index2-index1
        
        for z in range(len(temp)):
            t = int(temp[z])
            s += t
        Ss.append(s) 
    nums.append(num)   
#print(nums)

TSs = []
for k in range(len(Ss)):
    TS = Ss[k] + Ps[k]
    TSs.append(TS)

D1 = D1.tolist()
D2 = D2.tolist()
D3 = D3.tolist()

WSs = []
for k in range(len(nums)):
    WS = round(Ss[k] / nums[k],2)
    WSs.append(WS)

DataSet = {"Product_Name":D1,"Cost":D2,"Price":D3,"Market_share":MSs,"Price_Wage_Ratio":PWRs,
           "Price_Cost_Ratio":PCRs,"P_Score":Ps,"Total_Score":TSs,"Weighted_Score":WSs}
df = pd.DataFrame(DataSet)
df.to_csv('Products_Score.csv',index = False)

#score = P + F + C - D

